﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace _1._7线程优先级
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("currenty thread priority:{0}",Thread.CurrentThread.Priority);
            Console.WriteLine("-----------------------------");
            RunThread();
            Thread.Sleep(TimeSpan.FromSeconds(2));
            Console.WriteLine("+++++++++++++++++");
            Process.GetCurrentProcess().ProcessorAffinity = new IntPtr(1);
            RunThread();
        }
        static void RunThread() 
        {
            var sample = new ThreadSample();
            var t1 = new Thread(sample.CountNumbers);
            t1.Name = "t1";
            var t2 = new Thread(sample.CountNumbers);
            t2.Name = "t2";
            t1.Priority = ThreadPriority.Highest;
            t2.Priority = ThreadPriority.Lowest;
            t1.Start();
            t2.Start();
            Thread.Sleep(TimeSpan.FromSeconds(2));
            sample.Stop();

        }
    }

    class ThreadSample 
    {
        private bool _isStoped = false;
        public void Stop() 
        {
            _isStoped = true;
        }
        public  void CountNumbers() 
        {
            long counter = 0;
            while (!_isStoped)
            {
                counter++;
            }

            Console.WriteLine("{0} with {1,11} protity has a count={2,13}",Thread.CurrentThread.Name,Thread.CurrentThread.Priority,counter.ToString("N0"));
        }
    }

}
